#include "mycanvasmandelbrot.h"


MyCanvasMandelBrot::MyCanvasMandelBrot(QWidget *parent) : MyCanvas(parent)
{

}

void MyCanvasMandelBrot::paintEvent(QPaintEvent* pEvent)
{
    int p=30;
    QPainter painter(this);
    QSize size = this->size();
    painter.fillRect(0,0,size.width(), size.height(), Qt::black);
    painter.setPen(Qt::red);
    for(int x=0;x<this->size().width();x++)
    {
        for(int y=0;y<this->size().height();y++)
        {
            double xfuncion=x_funcion(x,&this->size());
            double yfuncion=y_funcion(y,&this->size());
            Complex c(xfuncion,yfuncion);
            Complex z(0,0);
            for(int i=1;i<=p;i++)
            {
                Complex znuevo=z*z+c;
                if(znuevo.moduloALa2()>4)
                {
                    painter.setPen(QColor((255/p)*i,0,0));
                    painter.drawPoint(x,y);
                    break;
                }
                z=znuevo;
            }
        }
    }

}

void MyCanvasMandelBrot::mousePressEvent(QMouseEvent *event)
{
    double a=x_funcion(event->x(),&this->size());
    double b=y_funcion(event->y(), &this->size());
    this->julia->c->real=a;
    this->julia->c->imaginario=b;
    this->julia->update();;
}
